package com.jcl.cloud.services.crawler.catchnew.service;


import com.jcl.cloud.common.service.constant.TaskRouteEnum;
import com.jcl.cloud.services.crawler.catchnew.constant.TaskInfo;
import com.jcl.cloud.services.crawler.catchnew.factory.CatchNewHandlerFactory;
import com.jcl.cloud.services.crawler.catchnew.task.CatchNewHandler;
import com.jcl.cloud.services.crawler.catchnew.task.CatchNewTaskWrap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CatchNewManagerService {

    @Autowired
    private CatchNewHandlerFactory catchNewHandlerFactory;

    @Autowired
    private ThreadPoolService threadPoolService;

    @Autowired
    private RedisService redisService;

    @Autowired
    private ThreadMonitorService threadMonitorService;


    public void createCatchNewTask(String crawlerId){

        String siteCode = this.redisService.hget("unicrawler:task:info:"+crawlerId,"siteCode");
        /*if(TaskInfo.runningTask.contains(crawlerId)){
            //放入任务锁的map中
            TaskInfo.taskLockMap.put(crawlerId,siteCode);
        }else{*/
            TaskRouteEnum taskRouteEnum = TaskRouteEnum.explain(siteCode);
            CatchNewHandler catchNewHandler = this.catchNewHandlerFactory.getHandler(taskRouteEnum);
            //this.threadPoolService.execute(new CatchNewTaskWrap(catchNewHandler,crawlerId));
            this.threadMonitorService.execute(new CatchNewTaskWrap(catchNewHandler,crawlerId),crawlerId);

      /*      TaskInfo.runningTask.put(crawlerId,siteCode);
        }*/
    }


}
